import { getConnection } from 'typeorm'
import { FindManyOptions } from 'typeorm'
import { KeyStore } from '@app/models/keystore'

const KeyStores = new class {
    async get(address: string) {
        const connection = getConnection('keystore')
        return await connection.getRepository(KeyStore).findOne({address})
    }

    async scan(offset: number, limit: number) {
        const options: FindManyOptions = {
            select: ['address'],
            order: {id: 'ASC'},
            skip: offset,
            take: limit
        }
        const connection = getConnection('keystore')
        return await connection.getRepository(KeyStore).find(options)
    }

    async insert(address: string, privateKey: string) {
        const record = new KeyStore()
        record.address = address
        record.privateKey = privateKey

        const connection = getConnection('keystore')
        return await connection.getRepository(KeyStore).insert(record)
    }
}()

export { KeyStores }
